<template>
  <a-cascader
    :options="dataList"
    :show-search="{ filter }"
    :fieldNames="fieldNames"
    placeholder="请选择"
    v-model="dataVal"
    @change="onChange"
  />
  <!-- <a-select :filter-option="filterOption" allow-clear placeholder="请选择" v-model="dataVal" @change="onChange">
    <a-select-opt-group v-for="(item, index) in dataList" :key="index" :label="item.name">
      <a-select-option v-for="(item2, index2) in item.companyAuthorityList" :key="index2" :value="item2.id">
        {{ item2.responsibleAuthorityName || "税务局" }}
      </a-select-option>
    </a-select-opt-group>
  </a-select> -->
</template>
<script>
import { getCompanyAuthoritiesListAll } from "../api";
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    val: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      dataVal: undefined,
      dataList: [],
      fieldNames: {
        children: "companyAuthorityList",
        label: "responsibleAuthorityName",
        value: "id"
      }
    };
  },
  watch: {
    visible() {
      this.getDataVal();
    }
  },
  created() {
    this.ongGtCompaniesListAll();
    this.getDataVal();
  },
  methods: {
    getDataVal() {
      if (this.val) {
        this.dataVal = [this.val];
      } else {
        if (this.visible) this.dataVal = undefined;
      }
    },
    onChange(value, selectedOptions) {
      this.$emit("upDate", value);
    },
    async ongGtCompaniesListAll() {
      try {
        const { data } = await getCompanyAuthoritiesListAll();
        this.dataList = data;
        this.dataList.map((e) => {
          e.responsibleAuthorityName = e.name;
          if (e.companyAuthorityList.length <= 0) e.disabled = true;
        });
      } catch (err) {
        console.log("error log --> ", err);
      }
    },
    filter(inputValue, path) {
      return path.some((option) => option.responsibleAuthorityName.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
    },
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
    }
  }
};
</script>
